<?php
namespace app\newapi\controller;

use think\Controller;
use think\Config;
use app\common\model\Kefu as KefuModel;

class Regist extends Controller{
    protected $kefu_model;

    protected function _initialize(){
        parent::_initialize();
        $this->kefu_model = new KefuModel;
    }

    /**
     * 获取账号下的客服列表
     */
    public function Regist(){
        if($this->request->isPost()){
            $data            = $this->request->post();
            $validate_result = $this->validate($data , 'kefu');
            if($validate_result !== true){
                return json(['status' => -1 , 'error' => $validate_result]);
            } else {
                $checkStr = $data['username'].','.$data['verify'];
                $checkVerify = checode($checkStr);
                if($checkVerify){
                    $data['password'] = md5($data['password'] . Config::get('salt'));
                    if ( $this->kefu_model->allowField(true)->save($data)) {
                        return json(['status' => -1 , 'msg' => 'ok']);
                    } else {
                        return json(['status' => -1 , 'error' => 'save failed']);
                    }
                }else{
                    return json(['status' => -1 , 'error' => 'verify misstake']);
                }

            }
        }
    }


}